package com.qianxx.component.activity.recycleview.list_view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qianxx.component.R;

import java.util.ArrayList;
import java.util.List;

public class OrdinaryListViewAdapter extends RecyclerView.Adapter<OrdinaryListViewAdapter.MyViewHolder> {
    OnItemClickListener listener;
    List<String> list = new ArrayList<>();
    Context context;

    public OrdinaryListViewAdapter(Context context,List<String> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder arg0, int arg1) {
        arg0.text.setText(list.get(arg1));
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.indexable_list_item, null);
        return new MyViewHolder(view, listener);
    }

    // 接收view的点击事件
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements
            View.OnClickListener {
        private TextView text;
        private OnItemClickListener listener;

        public MyViewHolder(View view, OnItemClickListener listener) {
            super(view);
            text = (TextView) view.findViewById(R.id.text);
            this.listener = listener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // 将view的点击事件传递出去
            if (listener != null) {
                listener.onItemClick(v, getPosition());
            }

        }

    }

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

}

